/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uvexposureapplication.rpc.clients;

import java.util.HashMap;
import java.util.Map;
import java.util.Observable;
import java.util.logging.Level;
import java.util.logging.Logger;
import uvexposureapplication.rpc.AbstractPseudoRPCClientStub;
import uvexposureapplication.server.ContextManager;

/**
 *
 * @author matthieukluj
 */
public class PreferenceRepositoryPseudoRPCClientStub extends AbstractPseudoRPCClientStub {

    ContextManager cManager;

    public PreferenceRepositoryPseudoRPCClientStub(ContextManager cManager, String methodName, String parameters, String server) {
        super(methodName, parameters, server);
        this.serverName = "PreferenceRepository";
        this.cManager = cManager;
    }

    @Override
    public void produce() {
        Map<String, String> notification = new HashMap<String, String>();
        notification.put("RPCServerName", this.serverName);
        notification.put("MethodName", this.methodName);
        if (this.parameters != null)
            notification.put("Parameters", this.parameters);
        notification.put("Side", "Client");
        this.producer.produceFromCollection(notification);
        synchronized (this) {
            try {
                this.wait();
            } catch (InterruptedException ex) {
                Logger.getLogger(LocationServerPseudoRPCClientStub.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        //this.producer.closeConnection();
        //this.consumer.closeConnection();
    }

    @Override
    public void subscribeToServer() {
        this.consumer.subscribe("RPCServerName == '" + this.serverName + "' "
                + "&& MethodName == '" + this.methodName + "'");

    }

    @Override
    public void update(Observable o, Object arg) {

        if (arg instanceof Map) {
            Map<String, String> argMap = (Map<String, String>) arg;

            if (argMap.get("RPCServerName").equals(this.serverName)) {

                if (argMap.get("MethodName").equals(this.methodName)) {
                    if (argMap.get("Side").equals("Server")) {
                        if (argMap.get("MethodName").equals("fetchUserPreference")) {
                            synchronized (this) {
                                this.notify();
                                this.cManager.setUserPreferences((String) argMap.get("Result"));
                            }
                        } else if (argMap.get("MethodName").equals("fetchUserPreferenceUV")) {
                            synchronized (this) {
                                this.notify();
                                this.cManager.produce((String) argMap.get("Result"), "fetchUserPreferenceUV");
                            }

                        } else if (argMap.get("MethodName").equals("fetchUserPreferenceTemp")) {
                            synchronized (this) {
                                this.notify();
                                this.cManager.produce((String) argMap.get("Result"), "fetchUserPreferenceTemp");
                            }
                        }
                    }
                }
            }
        }
    }
}
